package com.itsq.service.resources.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itsq.common.bean.ErrorEnum;
import com.itsq.common.constant.APIException;
import com.itsq.pojo.dto.PageDepartmentDto;
import com.itsq.pojo.entity.Company;
import com.itsq.pojo.entity.Department;
import com.itsq.mapper.DepartmentMapper;
import com.itsq.service.resources.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 史先帅
 * @since 2021-01-30
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired(required = false)
    private DepartmentMapper departmentMapper;

    @Override
    public Page<Department> selDepartmentPageFront(PageDepartmentDto pageDepartmentDto) {
        Page<Department> page=new Page<>(pageDepartmentDto.getPageIndex(),pageDepartmentDto.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
        if(pageDepartmentDto.getOrderByField()==null){
            queryWrapper.orderByDesc("cre_date");
        }else{
            queryWrapper.orderByDesc(pageDepartmentDto.getOrderByField());
        }
        return departmentMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Page<Department> selDepartmentPageAfter(PageDepartmentDto pageDepartmentDto) {
        Page<Department> page=new Page<>(pageDepartmentDto.getPageIndex(),pageDepartmentDto.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
        if(pageDepartmentDto.getOrderByField()==null&&pageDepartmentDto.getName()==null){
            queryWrapper.orderByDesc("cre_date");
        }else if(pageDepartmentDto.getName()!=null){
            queryWrapper.orderByDesc("cre_date");
            queryWrapper.like("name",pageDepartmentDto.getName());
        }else{
            queryWrapper.orderByDesc(pageDepartmentDto.getOrderByField());
        }
        return departmentMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Integer insert(Department department) {
        int i = departmentMapper.insert(department);
        if(i<1){
            throw new APIException(ErrorEnum.is_ISNULL);
        }
        return i;
    }

    @Override
    public Integer update(Department department) {
        int i = departmentMapper.updateById(department);
        if(i<1){
            throw new APIException(ErrorEnum.is_ISNULL);
        }
        return i;
    }

    @Override
    public List<Department> selDepartmentList() {
        QueryWrapper queryWrapper=new QueryWrapper();
        return departmentMapper.selectList(queryWrapper);
    }

    @Override
    public Department selDepartmentId(Integer id) {
        return departmentMapper.selectById(id);
    }

    @Override
    public List<Department> selDepartment(Integer companyId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("company_id",companyId);
        return departmentMapper.selectList(queryWrapper);
    }

    @Override
    public Integer delete(Integer id) {
        return departmentMapper.deleteById(id);
    }
}
